package com.x.crm.manage;

import com.alibaba.fastjson.JSON;
import com.x.crm.dao.CustomerDao;
import com.x.crm.dao.IntentionModelDao;
import com.x.crm.model.*;
import com.x.crm.util.Constant;
import com.x.crm.util.ConstantTopic;
import com.x.crm.payload.IntentionMsg;
import com.x.framework.manage.BaseManage;
import com.x.rocketmq.RocketMqProducer;
import org.apache.commons.lang.StringUtils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.x.crm.dao.IntentionDao;

import java.util.List;

@Service
public class IntentionManage extends BaseManage {

	private static final Logger logger = LoggerFactory.getLogger(IntentionManage.class);

	@Autowired
	private IntentionDao intentionDao;

	@Autowired
	private CustomerDao customerDao;

	@Autowired
	private IntentionModelDao intentionModelDao;
	@Autowired
	private RocketMqProducer rocketMqProducer;


	/**
	 * 新增微信用户询价记录
	 *
	 * @param intention
	 * @return
	 * @throws Exception
	 */
	public Intention insertWxIntentionInfo(Intention intention, IntentionMsg intentionMsg) throws Exception {
		List<IntentionModel> modelList = intention.getIntentionModelList();
		setModelName(intention, modelList);
		intention.setIntentionNo(this.getUid(15));
		Intention insertedIntention = this.insert(intention);
		if (modelList != null) {
			for (IntentionModel model : modelList) {
				model.setIntentionId(insertedIntention.getIntentionId());
				model.setCarState(Constant.CAR_VALID_STATE);
				this.insert(model);
			}
			if (intentionMsg != null) {
				rocketMqProducer.produceMessage(ConstantTopic.TOPIC_CRM_PUSH, ConstantTopic.TAG_PUSH_INTENTION, intentionMsg);
			}
		}
		return intention;
	}

	/**
	 * 201807版
	 * 新增客户信息以及客户意向
	 * @param intention
	 * @return
	 * @throws Exception
	 */
	public Intention insertCustomerIntentionInfo(Intention intention) throws Exception {
		List<IntentionModel> modelList = intention.getIntentionModelList();
		CustomerShop customerShop = intention.getCustomerShop();
		if (customerShop != null ) {
			Customer customer = customerShop.getCustomer();
			if (StringUtils.isNotEmpty(customerShop.getCustomerShopId())){
				this.update(customerShop);
			}else{
				List<Customer> customerList = customerDao.selectCustomerList(customer);
				if (customerList != null && !customerList.isEmpty()){
					customer = customerList.get(0);
				}else{
					customer.setCreateTime(this.getDate(0));
					customer.setUpdateTime(this.getDate(0));
					this.insert(customer);
				}
				customerShop.setCustomerId(customer.getCustomerId());
				this.insert(customerShop);
				intention.setCustomerShopId(customerShop.getCustomerShopId());
			}
		}
		setModelName(intention, modelList);
		intention.setIntentionNo(this.getUid(15));
		Intention insertedIntention = this.insert(intention);
		setModelList(insertedIntention,modelList);
		return insertedIntention;
	}



	/**
	 * 201807版
	 * 更新客户信息以及客户意向
	 * @param intention
	 * @return
	 * @throws Exception
	 */
	public int updateCustomerIntentionInfo(Intention intention) throws Exception {
		List<IntentionModel> modelList = intention.getIntentionModelList();
		CustomerShop customerShop = intention.getCustomerShop();
		this.update(customerShop);
		IntentionModel intentionModel = new IntentionModel();
		intentionModel.setIntentionId(intention.getIntentionId());
		intentionModelDao.delete(intentionModel);
		setModelName(intention, modelList);
		setModelList(intention, modelList);
		return this.update(intention);
	}


	/**
	 * 基础更新CUSTOMERSHOP和意向
	 * @param intention
	 * @return
	 * @throws Exception
	 */
	public int updateCustomerIntentionBasic(Intention intention) throws Exception {
		CustomerShop customerShop = intention.getCustomerShop();
		if (customerShop == null ){
			return  0;
		}
//		System.out.println(JSON.toJSON(customerShop));
		this.update(customerShop);
		return this.update(intention);
	}




	/**
	 * 新增跟进成交信息
	 * @param followFixture
	 * @return
	 * @throws Exception
	 */
	public FollowFixture insertFollowFixture(FollowFixture followFixture) throws Exception {
		List<FixtureCar> fixtureCarList = followFixture.getFollowFixtureCarList();
		List<FollowService> followServiceList = followFixture.getFollowServiceList();
		FollowFixture followFixtureInfo = this.insert(followFixture);
		if (fixtureCarList != null){
			for(FixtureCar fixtureCar : fixtureCarList){
				fixtureCar.setFollowFixtureId(followFixtureInfo.getFollowFixtureId());
				fixtureCar.setCarState(Constant.CAR_VALID_STATE);
				fixtureCar.setFixtureCreateTime(this.getDate(0));
				fixtureCar.setFixtureUpdateTime(this.getDate(0));
				this.insert(fixtureCar);
			}
		}
		if (followServiceList != null){
			for(FollowService followService : followServiceList){
				followService.setFollowServiceId(null);
				followService.setFollowType("followFixture");
				followService.setFollowTypeId(followFixtureInfo.getFollowFixtureId());
				followService.setServiceCreateTime(this.getDate(0));
				followService.setServiceUpdateTime(this.getDate(0));
				this.insert(followService);
			}
		}
		return followFixtureInfo;
	}





	/**
	 * 新增跟进预订信息
	 * @param followOrder
	 * @return
	 * @throws Exception
	 */
	public FollowOrder insertFollowOrder(FollowOrder followOrder) throws Exception {
		List<OrderCar> orderCarList = followOrder.getFollowOrderCarList();
		List<FollowService> followServiceList = followOrder.getFollowServiceList();
		FollowOrder followOrderInfo = this.insert(followOrder);
		if (orderCarList != null){
			for(OrderCar orderCar : orderCarList){
				orderCar.setFollowOrderId(followOrderInfo.getFollowOrderId());
				orderCar.setCarState(Constant.CAR_VALID_STATE);
				orderCar.setOrderCreateTime(this.getDate(0));
				orderCar.setOrderUpdateTime(this.getDate(0));
				this.insert(orderCar);
			}
		}
		if (followServiceList != null){
			for(FollowService followService : followServiceList){
				followService.setFollowType("followOrder");
				followService.setFollowTypeId(followOrderInfo.getFollowOrderId());
				followService.setServiceCreateTime(this.getDate(0));
				followService.setServiceUpdateTime(this.getDate(0));
				this.insert(followService);
			}
		}
		return followOrderInfo;
	}





	private void setModelName(Intention intention, List<IntentionModel> modelList) {
		if (modelList != null && modelList.size() > 0) {
			String modelName = modelList.get(0).getBrandName() + " " + modelList.get(0).getSeriesName();
			if (modelList.size() > 1) {
				modelName = modelName + " 等";
			}
			intention.setModelName(modelName);
		}
	}


	private void setModelList(Intention intention, List<IntentionModel> modelList) throws Exception{
		if (modelList != null) {
			for (IntentionModel model : modelList) {
				model.setIntentionId(intention.getIntentionId());
				model.setCarState(Constant.CAR_VALID_STATE);
				this.insert(model);
			}
		}
	}

	public int update(List<Intention> list) {
		int cnt = 0;
		for (Intention intention : list) {
//            Intention where = new Intention();
//            where.setIntentionId(intention.getIntentionId());
//            Intention set = new Intention();
//            set.setIntentionState(intention.getIntentionState());
//            set.setFollowerId(intention.getFollowerId());
//            set.setNextFollowDatetime(intention.getNextFollowDatetime());
			cnt += intentionDao.update(intention);
		}
		return cnt;
	}

	//public int updateIntention(Intention intentionSet, Intention intentionWhere) throws Exception {
//        Customer customer = intentionSet.getCustomer();
//        Customer customerWhere = new Customer();
//        customerWhere.setCustomerId(intentionSet.getCustomerId());
//        log.info("customer = " + JSON.toJSONString(customer));
//
//        customerDao.update(customer, customerWhere);
//        IntentionModel intentionModel = new IntentionModel();
//        intentionModel.setIntentionId(intentionWhere.getIntentionId());
//        intentionModelDao.delete(intentionModel);
//        List<IntentionModel> modelList = intentionSet.getIntentionModelList();
//        setModelName(intentionSet, modelList);
//        if(modelList!= null){
//            for(IntentionModel model : modelList){
//                model.setIntentionId(intentionWhere.getIntentionId());
//                this.insert(model);
//            }
//        }
//        log.info("intentionSet = " + JSON.toJSONString(intentionSet));
	//return intentionDao.update(intentionSet, intentionWhere);
	//}


	public int updateIntentionInfo(Intention intention) throws Exception {
		IntentionModel intentionModel = new IntentionModel();
		intentionModel.setIntentionId(intention.getIntentionId());
		intentionModelDao.delete(intentionModel);
		List<IntentionModel> modelList = intention.getIntentionModelList();
		setModelName(intention, modelList);
		if (modelList != null) {
			for (IntentionModel model : modelList) {
				model.setIntentionId(intention.getIntentionId());
				model.setCarState(1);
				this.insert(model);
			}
		}
		logger.info("intentionSet = " + JSON.toJSONString(intention));
		return this.update(intention);
	}
}
